export default {
	data() {
		return {
			typeIndex: -1,
			typeSelect: [{
				type: '1',
				text: '判断题'
			}, {
				type: '2',
				text: '单选题'
			}, {
				type: '3',
				text: '多选题'
			}, {
				type: '4',
				text: '填空题'
			}, {
				type: '5',
				text: '问答题'
			}],
			judgeIndex: -1,
			judgeSelect: [{
				type: 'A',
				text: '对'
			}, {
				type: 'B',
				text: '错'
			}],
			choiceIndex: -1,
			choiceSelect: [{
				text: 'A'
			}, {
				text: 'B'
			}, {
				text: 'C'
			}, {
				text: 'D'
			}],
			type: '',
			title: '',
			optionList: [{
				id: 'A',
				content: ''
			}, {
				id: 'B',
				content: ''
			}, {
				id: 'C',
				content: ''
			}, {
				id: 'D',
				content: ''
			}],
			answer: '',
			explain: '',
			isSubject: false,
			loadModal: false,
		};
	},
	methods: {
		TypeChange(e) {
			const num = parseInt(e.detail.value);
			
			if (num === -1) {
				this.typeIndex = 0;
			} else {
				this.typeIndex = num;
			}
			// 这个值是字符串，当放进数组是字符串会报错说数组不是object对象，因为数组下标要用数字
			this.type = this.typeSelect[this.typeIndex].type;			
		},
		JudgeChange(e) {
			const num = parseInt(e.detail.value);
			if (num === -1) {
				this.judgeIndex = 0;
				this.answer = this.judgeSelect[0].type;
			} else {
				this.judgeIndex = num;
				this.answer = this.judgeSelect[num].type;
			}			
		},
		ChoiceChange(e) {
			const num = parseInt(e.detail.value);
			if (num === -1) {
				this.choiceIndex = 0;
				this.answer = 'A';
			} else {
				this.choiceIndex = num;
				this.answer = this.choiceSelect[num].text;
			}
		},
		CheckChioceChange(e){
			var values = e.detail.value;
			this.answer = "";
			for (var i = 0; i < values.length; i++) {
				this.answer = this.answer + values[i];
			}			
		},
		IsSubject(e) {
			this.isSubject = e.detail.value;
		},
		ShowToast(msg) {
			uni.showToast({
			    title: msg,
			    duration: 2000
			});
		},
		CheckForm() {
			if (this.type === '') {
				this.ShowToast('请选择题型');
				return false;
			}

			if (this.title === '') {
				this.ShowToast('题目不能为空');
				return false;
			}

			if (this.answer === '') {
				this.ShowToast('请输入答案');
				return false;
			}

			if (this.explain === '') {
				this.ShowToast('请输入解析');
				return false;
			}

			if (this.type === '2' || this.type === '3') {
				for (let i = 0; i < 4; i++) {
					if (this.optionList[i].content === '') {
						this.ShowToast('选项' + this.optionList[i].id + '不能为空');
						return false;
					}
				}
			}

			return true;

		},
		Submit(e) {
			this.loadModal = true;

			if (!this.CheckForm()) {
				this.loadModal = false;
				return;
			}

			const optionList = this.setOption(this.type);
			const sendData = {
				type: this.type,
				title: this.title,
				optionList: optionList,
				answer: this.answer,
				explain: this.explain,
				isSubject: this.isSubject
			};

			uniCloud.callFunction({
				name: 'setSubject',
				data: {
					subject: sendData
				},
				success: (res) => {
					this.ShowToast('上传成功');
					this.ResetForm();
					this.loadModal = false;
				},
				fail: (res) => {
					console.log('fail:' + res);
					this.ShowToast('上传失败');
					this.loadModal = false;
				},
				complete: (res) => {
					console.log('complete:' + res);
				}
			});
		},
		setOption(type) {
			if (type === '1') {
				return [{
						"content": "对",
						"id": "A"
					},
					{
						"content": "错",
						"id": "B"
					}
				];
			} else if (type === '2' || type === '3') {
				return this.optionList;
			} else {
				return [];
			}
		},
		ResetForm(){
			this.typeIndex = -1;
			this.judgeIndex = -1;
			this.choiceIndex = -1;
			this.type = '';
			this.title = '';
			this.optionList = [{
				id: 'A',
				content: ''
			}, {
				id: 'B',
				content: ''
			}, {
				id: 'C',
				content: ''
			}, {
				id: 'D',
				content: ''
			}];
			this.answer = '';
			this.explain = '';
			this.isSubject = false;
		}
	}
}
